package com.eliza.lifecycle.newControls

import android.content.Context
import android.os.SystemClock
import android.util.AttributeSet
import android.widget.Chronometer
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent


/**
 * 自定义控件 继承LifecycleObserver  解耦合
 */
class NewChronometer(context: Context?, attrs: AttributeSet?) : Chronometer(context, attrs),
    LifecycleObserver {
    var elapsedTime: Long = 0

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    fun onStart() {
        base = SystemClock.elapsedRealtime() - elapsedTime //开机时间
        start()
    }
    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    fun onStop() {
        elapsedTime = SystemClock.elapsedRealtime() - base //开机时间
        stop()
    }
}